/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Kilian Ito
 */
public class ListWord {
    public final static String WordFolder = "." + File.separator + "Words";
    private String w = "";
    private String pathw = "";
    private int version = -1;
    private int lastVersion = -1;
    public ListWord()
    {
        // pas obligatoire
        File f = new File(WordFolder);
        if (!f.exists())
            f.mkdir();
    }

    // Mot traite
    public void setWord(String word)
    {
        w = ""+word;
        setConvertedWord(w);
    }
    public String getWord()
    {
        return w;
    }

    // Le mot existe
    public void setExist(boolean b)
    {
        
    }
    public boolean getExist()
    {
        File folder = new File(WordFolder + File.separator + getConvertedWord());
        if (!(folder.exists() && folder.isDirectory()))
            return false;

        File html = new File(WordFolder + File.separator + getConvertedWord() + File.separator + "index.html");
        return html.exists();
    }

    // Selectionne la version a editer
    public void setSelectVersion(int i)
    {
        if (i <= getLastVersion())
            version = i;
    }
    public int getSelectVersion()
    {
        return version;
    }

    // derniere version en cours
    public int getLastVersion()
    {
        File folder = new File(WordFolder + File.separator + getConvertedWord());
        return folder.listFiles(new FileFilter(){
            @Override
            public boolean accept(File pathname) {
                return pathname.getName().matches("[0-9]*.txt");
            }
        }).length - 1;
    }
    public void setLastVersion(int i)
    {

    }

    // derniere version actuelle (lors de l'edition)
    public int getMyLastVersion()
    {
        return lastVersion;
    }
    public void setMyLastVersion(int i)
    {
        
    }

    // mot traité
    public List<String> getWords(){
        ArrayList<String> res = new ArrayList<String>();
        File folder = new File(WordFolder);
        for (File f :folder.listFiles(new FileFilter(){
            @Override
            public boolean accept(File pathname) {
                return pathname.isDirectory();
            }
        }))
            res.add(reverseConvertWord(f.getName()));
        return res;
    }
    public void setWords(List<String> customers)
    {

    }

    // text du dernier version
    public String getText()
    {
        lastVersion = getLastVersion();
        return readFile(WordFolder + File.separator + getConvertedWord() + File.separator + version + ".txt");
    }
    public void setText(String text)
    {
        if (!getExist())
        {
            File f = new File(WordFolder + File.separator + getConvertedWord());
            f.mkdir();
        }
        saveVersion(text);
        saveHTML(text);
    }

     // HTML du dernier version
    public String getHtml()
    {
        return readFile(WordFolder + File.separator + getConvertedWord() + File.separator + "index.html");
    }
    public void setHtml(String text)
    {
        
    }

    // convertir le mot en nom pour un r�pertoire
    public String getConvertedWord()
    {
        return pathw;
    }
    public void setConvertedWord(String word)
    {
        pathw = w.replaceAll(" ", "_");
    }

    // ecrire dans un fichier
    private void writeFile(String fichier, String text)
    {
        try
        {
            FileWriter fw = new FileWriter(fichier, true);
            BufferedWriter output = new BufferedWriter(fw);
            output.write(text);
            output.flush();
            output.close();
        }
        catch(IOException ioe){
            
        }
    }

    // Lire le contenu d'un fichier
    private String readFile(String fichier)
    {
        String chaine="";

        try{
            InputStream ips=new FileInputStream(fichier);
            InputStreamReader ipsr=new InputStreamReader(ips);
            BufferedReader br=new BufferedReader(ipsr);
            String ligne;
            while ((ligne=br.readLine())!=null)
                    chaine+=ligne + System.getProperty("line.separator");
            br.close();
        }
        catch (Exception e){
                
        }
        return chaine;
    }

    // convertir le code Wiki en page HTML
    private String convertText(String text)
    {
        String res = "" + text;
        res = res.replaceAll("(http://[^ ]*)", "<a href=\"$1\">$1</a>");
        res = res.replaceAll("\\[([^\\[\\]]*)\\]", "<a href=\"./$1\">$1</a>");
        res = res.replaceAll("\\+\\+(.*)\\+\\+", "<bold>$1</bold>");
        res = res.replaceAll("\\+([^\\+]*)\\+", "<i>$1</i>");
        res = res.replaceAll("^!!! (.*)$", "<h3>$1</h3>");
        res = res.replaceAll("^!! (.*)$", "<h2>$1</h2>");
        res = res.replaceAll("^! (.*)$", "<h1>$1</h1>");
        res = res.replaceAll("^---$", "<hr />");
        return res;
    }

    // Enregistrer la page web
    private void saveHTML(String text)
    {
        writeFile(WordFolder + File.separator + getConvertedWord() + File.separator + "index.html", convertText(text));
    }

    // Enregistrer la derniere version
    private void saveVersion(String text)
    {
        writeFile(WordFolder + File.separator + getConvertedWord() + File.separator + (getLastVersion() + 1) + ".txt", text);
        lastVersion = getLastVersion();
        version = getLastVersion();
    }

    // R�cup�rer le mot d'origine
    private String reverseConvertWord(String word)
    {
        return word.replaceAll("_", " ");
    }
}
